London | 25-SDC-Nov | Aida Eslamimoghadam | Sprint 4 | Implement Shell tools in python #270
London | 25-SDC-Nov | Aida Eslamimoghadam | Sprint 4 | Implement Shell tools in python #270aydaeslami wants to merge 9 commits intoCodeYourFuture:mainfrom
Conversation
DaryaShirokova
left a comment
There was a problem hiding this comment.
Great progress on this! Left a few comments here
implement-shell-tools/cat/cat.py
Outdated
| description="Simple cat clone with -n and -b options", | ||
| ) | ||
|
|
||
| counterNumber = 1 |
There was a problem hiding this comment.
In python the usual code style is to use snake_case.
There was a problem hiding this comment.
Thanks for pointing that out. I’ll update the variable.
implement-shell-tools/cat/cat.py
Outdated
| print(counterNumber, arrText[i]) | ||
| counterNumber += 1 | ||
| else: | ||
| print(content) No newline at end of file |
There was a problem hiding this comment.
Could you please wrap you logic into a method and run it using if __name__ == "__main__": approach? Ideally the program should not contain global vars, and having methods improves readability.
There was a problem hiding this comment.
Thanks for the suggestion. I’ve moved the logic into functions, added a main() entry point using if name == "main": and removed global variables to improve readability.
implement-shell-tools/cat/cat.py
Outdated
| ) | ||
|
|
||
| counterNumber = 1 | ||
| parser.add_argument("-n", action="store_true", help="number all lines") |
There was a problem hiding this comment.
cat -n sample-files/1.txt, cat.py sample-files/ *.txt (including -n option) and cat -b sample-files/3.txt all show slightly different result from yours.
implement-shell-tools/cat/cat.py
Outdated
| for file_path in args.path: | ||
| with open(file_path, "r") as f: | ||
| content = f.read() | ||
| arrText = content.split("\n") |
There was a problem hiding this comment.
There is a method in python to read file into an array of lines directly.
There was a problem hiding this comment.
Thank you for the suggestion. I’ll use the built-in method to read the file into a list of lines directly.
implement-shell-tools/cat/cat.py
Outdated
| arrText = content.split("\n") | ||
|
|
||
| if args.b: | ||
| for i in range(len(arrText )): |
There was a problem hiding this comment.
You can potentially use enumerate here.
| parser.add_argument("path", nargs="?", default=".", help="The directory to list") | ||
| args = parser.parse_args() | ||
|
|
||
|
|
There was a problem hiding this comment.
Again, formatting could be improved with ruff here or other formatters :)
There was a problem hiding this comment.
Thank you for the suggestion @DaryaShirokova.
I’ve formatted the code using ruff to improve consistency and readability.
implement-shell-tools/ls/ls.py
Outdated
| ) | ||
|
|
||
| parser.add_argument("-a", action="store_true", help="include hidden files") | ||
| parser.add_argument("-1", dest="one" ,action="store_true", help="use a long listing format") |
There was a problem hiding this comment.
What do you mean by long listing format here?
There was a problem hiding this comment.
Many thanks for your time and help.
I’ll update the help text to reflect that.
implement-shell-tools/ls/ls.py
Outdated
| import argparse | ||
| import os | ||
|
|
||
| def show_unhidden_files(listDir): |
There was a problem hiding this comment.
Can these two functions be merged into one using extra method param?
There was a problem hiding this comment.
Thanks for the suggestion.
I merged them into a single function with an extra parameter.
implement-shell-tools/wc/wc.py
Outdated
| parser.add_argument("path", nargs="+", default=".", help="The file to count") | ||
| args = parser.parse_args() | ||
|
|
||
| if not args.l and not args.w and not args.c: |
There was a problem hiding this comment.
Can you rework this code to have less duplication, by using methods?
There was a problem hiding this comment.
Thank you for your feedback, I’ve refactored the code to reduce duplication.
| @@ -0,0 +1,66 @@ | |||
| import argparse | |||
| parser = argparse.ArgumentParser( | |||
There was a problem hiding this comment.
Please enasure your output is the same as the one suggested for examples in the readme (it is ok if whitespaces are different in your output):
It must act the same as `wc` would, if run from the directory containing this README.md file, for the following command lines:
* `wc sample-files/*`
* `wc -l sample-files/3.txt`
* `wc -w sample-files/3.txt`
* `wc -c sample-files/3.txt`
* `wc -l sample-files/*`
implement-shell-tools/cat/cat.py
Outdated
| if args.b: | ||
| for line in lines: | ||
| if line.strip() != "": | ||
| print(counter_number, line, end="") | ||
| counter_number += 1 | ||
| else: | ||
| print(line, end="") | ||
|
|
||
| elif args.n: | ||
| for line in lines: | ||
| print(counter_number, line, end="") | ||
| counter_number += 1 | ||
|
|
||
| else: | ||
| for line in lines: | ||
| print(line, end="") |
There was a problem hiding this comment.
This works, but often when doing this kind of "there are a few ways we may want to change things, but we're doing the same thing at its core", we try to structure code to emphasise what's the same/different, so something like:
for line in lines:
prefix = ""
if args.n or (args.b and line.strip() != ""):
prefix = counter_number
counter_number += 1
print(f"{prefix} {line}")This makes clear that regardless of flags, we're doing something for each line, we're printing the line, and we may print something different at the start.
It particularly helps when we end up with lots of different combinations, to think how each affects what we're doing, rather than having to handle them all separately.
There was a problem hiding this comment.
Thank you @illicitonion. that’s a really helpful suggestion. I’ve refactored the code to emphasise the shared logic and handle the differences through a prefix, as you described.
implement-shell-tools/ls/ls.py
Outdated
| if show_hidden: | ||
| print(file) | ||
| else: | ||
| if not file.startswith("."): | ||
| print(file) |
There was a problem hiding this comment.
Same idea here:
if show_hidden or not file.startswith("."):
print(file)This way if we change how we print things, we only need to change it in once place, not two.
There was a problem hiding this comment.
Thank you for the suggestion @illicitonion. That was a really valuable point.
I have refactored the code to centralise the printing logic.
Many thanks.
implement-shell-tools/ls/ls.py
Outdated
| args = parser.parse_args() | ||
|
|
||
|
|
||
| fn = args.path |
There was a problem hiding this comment.
What does the variable name fn mean?
implement-shell-tools/ls/ls.py
Outdated
| fn = args.path | ||
| listDir = os.listdir(fn) | ||
|
|
||
| if fn != "": |
There was a problem hiding this comment.
When would fn be empty? From the naming here, it's pretty hard to work out what this means.
There was a problem hiding this comment.
You're right, Thank you for your feedback @illicitonion .
Since the path argument defaults to ".", the condition would never be false, so I have removed the unnecessary check.
I’ve also renamed the variable to make it clearer that it represents a directory path.
implement-shell-tools/wc/wc.py
Outdated
| if len(args.path) > 1 and not args.l and not args.w and not args.c: | ||
| print(lineCounter, wordCounter, charCounter, "total") No newline at end of file |
There was a problem hiding this comment.
The real wc shows totals even if you specify flags, e.g. wc -c /file/1 /file/2 will show a total row.
There was a problem hiding this comment.
Thank you for your feedback, Daniel. I have refactored my code accordingly. please let me know if any further changes are needed. I really appreciate your suggestion.
This PR includes my solutions to simulate several shell tools in Python.
I used the argparse module to re-implement them.